
package com.cortex.carambola.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ViewFlipper;

import com.cortex.carambola.summary.CarambolaRequestSummary;
import com.cortex.carambola.summary.CarambolaSummary;
import com.cortex.carambola.view.BrowserView;
import com.cortex.carambola.view.BrowserView.ActionOnBrowser;
import com.cortex.carambola.view.BrowserView.OnActionBrowserListener;
import com.cortex.carambola.view.HeaderView;
import com.cortex.carambola.view.HeaderView.ActionOnHeader;
import com.cortex.carambola.view.HeaderView.OnActionHeaderListener;
import com.cortex.carambola.view.ShowSummariesView;
import com.cortex.carambolasummary.CarambolaApplication;
import com.cortex.carambolasummary.CarambolaApplication.OnStateSummaryChanged;
import com.cortex.carambolasummary.R;



public class MainActivity extends Activity {

	private HeaderView headerView;

	private BrowserView browserView;
	private CarambolaApplication carambolaApp;
	private ViewFlipper flipperMain;
	private ShowSummariesView homeShowSummaryList,seasonShowSummaryList;
	private StateActivityViewEnum stateActivity = StateActivityViewEnum.HOME;
	private final static String KEY_CURRENT_VIEW_STATE = "currentViewState";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		carambolaApp = (CarambolaApplication) getApplication();
		initActivity();
		
	}
	@Override
	protected void onResume() {
		super.onResume();
		showView(stateActivity);
	}
	@Override
	public void onBackPressed(){
		if(flipperMain.getCurrentView().getId() == browserView.getId()){
			if (browserView.isCanGoBack()) {
				browserView.goBack();   
				return;
			}
			super.onBackPressed();
		}
		else{
			showView(StateActivityViewEnum.BROWSER);
		}
	}
	@Override
	protected void onSaveInstanceState(Bundle outState){
		super.onSaveInstanceState(outState);
		// Save the state of the WebView
		browserView.saveState(outState);
		outState.putInt(KEY_CURRENT_VIEW_STATE, stateActivity.ordinal());
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState){
		super.onRestoreInstanceState(savedInstanceState);
		// Restore the state of the WebView
		stateActivity = StateActivityViewEnum.values()[savedInstanceState.getInt(KEY_CURRENT_VIEW_STATE)];
		browserView.restoreState(savedInstanceState);
		showView(stateActivity);
	}
	private void initActivity(){
		initViewsReference();
		initViewsListener();
//		carambolaApp.timeLoadFinished(CarambolaSummary.createTestCarambolaSummaryFlashInMain());
//		carambolaApp.timeLoadFinished(CarambolaSummary.createTestCarambolaSummaryFlashInArticle());
//		carambolaApp.timeLoadFinished(CarambolaSummary.createTestCarambolaSummaryMp4());
//		carambolaApp.timeLoadFinished(CarambolaSummary.createTestCarambolaSummaryYoutube());
//		carambolaApp.timeLoadFinished(CarambolaSummary.createTestCarambolaSummaryMusic());

	}
	private void initViewsReference(){
		headerView = (HeaderView)findViewById(R.id.viewHeader);
		flipperMain = (ViewFlipper)findViewById(R.id.flipperMain);
		browserView = (BrowserView)findViewById(R.id.browserView);
		homeShowSummaryList = (ShowSummariesView)findViewById(R.id.homeSummaryListView);
		// to allow load url of summary
		homeShowSummaryList.setBrowser(browserView);

		seasonShowSummaryList = (ShowSummariesView)findViewById(R.id.seasonSummaryListView);
		// to allow load url of summary
		seasonShowSummaryList.setBrowser(browserView);
	}



	private void initViewsListener(){
		headerView.setOnActionHeaderListener(new OnActionHeaderListener() {
			@Override
			public void onAction(ActionOnHeader action) {
				switch(action){
				case BACK:
					browserView.goBack();
					break;
				case FORWARD:
					browserView.goForward();
					break;
				case LOGO:
					// set home display
					showView(StateActivityViewEnum.HOME);
					break;
				case SEASON_SUMMRIES:
					showView(StateActivityViewEnum.SEASON);
					break;
				case REFRESH:
					browserView.reload();
					break;
				case ROW_SEARCH_TO_LEFT:
					break;
				case ROW_SEARCH_TO_RIGHT:
					browserView.loadUrl(headerView.getRowSearchText());
					break;
				default:
					break;
				}
			}
		});

		carambolaApp.setOnStateSummaryChanged(new OnStateSummaryChanged() {

			@Override
			public void summaryTimeLoadStarted() {
				// when user long clicked on url this method called from javascript code 
				// and this method not run on ui thread so we need to run on ui thread
				MainActivity.this.runOnUiThread(new Runnable() {

					@Override
					public void run() {
						headerView.showProgressBar();
					}
				});

			}

			@Override
			public void summaryTimeLoadFinished(CarambolaSummary summary,boolean isOnlineRequestSummary) {
				if(!isOnlineRequestSummary){
					headerView.showSummarySucceed(summary.isSummarySuccess());
					return;
				}
				headerView.hideProgressBar();
				browserView.showSummaryView(summary);
			}

			@Override
			public void summaryTimeLoadCanceled(String message) {
				headerView.hideProgressBar();
			}
		});

		browserView.setOnActionBrowserListener(new OnActionBrowserListener() {

			@Override
			public void onAction(ActionOnBrowser action,String url) {
				switch(action){
				case CLOSE_SUMMARY_VIEW:
					break;
				case LOAD_PAGE_FINISHED:
					if(stateActivity == StateActivityViewEnum.BROWSER){
						headerView.hideProgressBar();
						headerView.setClickableBack(browserView.isCanGoBack());
						headerView.setClickableForward(browserView.isCanGoForward());
					}
					break;
				case LOAD_PAGE_STARTED:
					
					showView(StateActivityViewEnum.BROWSER);
					if(stateActivity == StateActivityViewEnum.BROWSER){
						headerView.setRowSearchText(url);
						headerView.showProgressBar();
						headerView.setClickableBack(browserView.isCanGoBack());
						headerView.setClickableForward(browserView.isCanGoForward());
					}
					break;
				case QUESTION_SUMMARY_IGONRE:
					break;
				case QUESTION_SUMMARY_YES:
					runRequestSummary(new CarambolaRequestSummary(url),true);
					break;
				case SHOW_SUMMARY_ONLINE:
					runRequestSummary(new CarambolaRequestSummary(url),true);
					break;
				case SHOW_SUMMARY_VIEW:
					runRequestSummary(browserView.getRequestSummary(),true);
					break;
				case LONG_CLICK_ON_URL:
					runRequestSummary(new CarambolaRequestSummary(url),false);
				default:
					break;

				}
			}
		});
	}
	private void runRequestSummary(CarambolaRequestSummary requestSummary,boolean isOnlineSummary){
		carambolaApp.runSummary(requestSummary,isOnlineSummary);
	}
	private void showView(StateActivityViewEnum state){
		String emptyListText,fullListText;
		switch(state){
		case BROWSER:
			if(browserView.isLoding()){
				headerView.showProgressBar();
			}
			break;
		case HOME:
			emptyListText = getResources().getString(R.string.emptyHistorySummaryList);
			fullListText = getResources().getString(R.string.historySummaryList);
			homeShowSummaryList.show(carambolaApp.getDataBaseSummaries(),fullListText,emptyListText);
			headerView.hideProgressBar();
			break;
		case SEASON:
			emptyListText = getResources().getString(R.string.emptySeasonSummaryList);
			fullListText = getResources().getString(R.string.seasonSummaryList);
			seasonShowSummaryList.show(carambolaApp.getSeasonSummaries(),fullListText,emptyListText);
			headerView.hideProgressBar();
			break;
		default:
			break;

		}
		flipperMain.setDisplayedChild(state.ordinal());
		stateActivity = state;
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	public enum StateActivityViewEnum{
		HOME,
		SEASON,
		BROWSER;

		public int home(){
			return StateActivityViewEnum.HOME.ordinal();
		}
		public int season(){
			return StateActivityViewEnum.SEASON.ordinal();
		}
		public int browser(){
			return StateActivityViewEnum.BROWSER.ordinal();
		}
	}
}
